package com.glorysoft.tonglingxjpda.ui.order.view

import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.ArrayAdapter
import com.glorysoft.tonglingxjpda.entity.ui.ToastMessageBean
import com.glorysoft.tonglingxjpda.BR
import com.glorysoft.tonglingxjpda.R
import com.glorysoft.tonglingxjpda.base.BaseActivity
import com.glorysoft.tonglingxjpda.databinding.ActivityOrderMangerDlBinding
import com.glorysoft.tonglingxjpda.entity.BomEntity
import com.glorysoft.tonglingxjpda.ui.PhotoActivity
import com.glorysoft.tonglingxjpda.ui.function.view.KYHInfoActivity
import com.glorysoft.tonglingxjpda.ui.order.viewmodel.OrderMangerDLViewModel
import com.glorysoft.tonglingxjpda.utils.PopWindowsUtils
import com.pda.platform.ui.ui_pdaplatform.datepicker.CustomDatePicker
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_DateUtils
import kotlinx.android.synthetic.main.activity_order_manger_dl.*
import kotlinx.android.synthetic.main.activity_title.*

/**
 * FileName: OrderMangerActivity
 * Author: XJH
 * Date: 2021/7/16 17:03
 * Description:
 * History:
 */
class OrderMangerDLActivity : BaseActivity<OrderMangerDLViewModel, ActivityOrderMangerDlBinding>(),
    View.OnClickListener {

    private var mDatePicker: CustomDatePicker? = null  //日期选择器
    private var dateType = 0
    private val isUrgent = arrayListOf("是", "否")
//    private val clearFiled = arrayListOf("铜", "非铜")
    private var urgentPop: PopWindowsUtils.GeneralPop? = null
    private var filedPop: PopWindowsUtils.GeneralPop? = null
    private var customPop: PopWindowsUtils.GeneralPop? = null
    private var bomPop: PopWindowsUtils.GeneralPop? = null
    private var docId : String? = null

    override fun layoutId(): Int {
        return R.layout.activity_order_manger_dl
    }

    override fun initVariableId(): Int {
        return BR.orderMangerDLViewModel
    }

    override fun initView(savedInstanceState: Bundle?) {
        docId = intent.getStringExtra("DOCID")
        viewModel.init(docId ?: "")
        setTitle("订单新增")
        setRightListener("保存") {
            if (tvUrgent.text.toString() == "是" && viewModel.orderEntity.value!!.PO.RESERVED4.isEmpty()){
                viewModel.defUI.toastEvent.value = ToastMessageBean("加急必须输入纳期",false)
                return@setRightListener
            }

            if (tvInTime.text.toString().isEmpty() || tvUrgent.text.toString().isEmpty()){
                viewModel.defUI.toastEvent.value = ToastMessageBean("受入日期或是否加急不能为空",false)
                return@setRightListener
            }
            viewModel.save("POSAVE")
        }

        tvRight2.text = "修改"
        tvRight2.visibility = View.VISIBLE
        tvRight2.setOnClickListener{
            if (tvUrgent.text.toString() == "是" && viewModel.orderEntity.value!!.PO.RESERVED4.isEmpty()){
                viewModel.defUI.toastEvent.value = ToastMessageBean("加急必须输入纳期",false)
                return@setOnClickListener
            }

            if (tvInTime.text.toString().isEmpty() || tvUrgent.text.toString().isEmpty()){
                viewModel.defUI.toastEvent.value = ToastMessageBean("受入日期或是否加急不能为空",false)
                return@setOnClickListener
            }
            viewModel.save("POUPDATE")
        }
        rvProd.setEmptyView(prodEmptyView)
//        flInTime.setOnClickListener(this)
        flCreateTime.setOnClickListener(this)
//        flOutTime.setOnClickListener(this)
        flUrgent.setOnClickListener(this)
        flFiled.setOnClickListener(this)
        flCustom.setOnClickListener(this)
        flBom.setOnClickListener(this)

        viewModel.defUI.callObserve.observe(this){
            when(it.type){
                0 ->{

                    if (docId == null){
                        viewModel.defUI.toastEvent.value = ToastMessageBean("新增订单不能查看刻印号",false)
                        return@observe
                    }
                    val entity = it.item as BomEntity
                    val bundle = Bundle()
                    bundle.putString("POID",viewModel.orderEntity.value!!.PO.DOCID)
                    bundle.putSerializable("DATA",entity)
//                    startActivity(KYHInfoActivity::class.java,bundle)
                    val intent = Intent(this,KYHInfoActivity::class.java)
                    intent.putExtras(bundle)
                    startActivityForResult(intent,200)
//            finish()
                }
                1 ->{
                    showLoading()
                }
                2 ->{
                    dismissLoading()
                }
                3->{
                    if (viewModel.orderEntity.value!!.PO.PLANSTARTDATE.isEmpty()){
                        tvInTime.text = FreeApi_DateUtils.getNowStringDate("yyyy/MM/dd HH:mm:ss")
                    }

                    if (viewModel.orderEntity.value!!.PO.RESERVED5.isEmpty()){
                        tvUrgent.text = "否"
                    }
                }
                200 ->{
                    val entity = it.item as BomEntity
                    val bundle = Bundle()
                    bundle.putString("PATH",entity.localPath)
                    startActivity(PhotoActivity::class.java,bundle)
                }
            }

        }

        viewModel.defUI.startActivity.observe(this){
            finish()
        }

    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == RESULT_OK){
            viewModel.init(docId ?: "")
        }
    }

    override fun initData() {
        viewModel.getCustom()
        val beginTimestamp = FreeApi_DateUtils.str2Long("1992-03-20", false)
        val endTimestamp = FreeApi_DateUtils.str2Long("2222-02-22", false)
        mDatePicker = CustomDatePicker(this, { timestamp ->
            val date = FreeApi_DateUtils.longToString(timestamp, "yyyy/MM/dd")
            when (dateType) {
                1 -> {
                    tvInTime.text = date
                }
                2 -> {
                    tvCreateTime.text = date
                }
//                3 -> tvOutTime.text = date
            }

        }, beginTimestamp, endTimestamp)
        mDatePicker!!.setCancelable(true)
        mDatePicker!!.setCanShowPreciseTime(false)
        mDatePicker!!.setScrollLoop(false)
        mDatePicker!!.setCanShowAnim(false)

        val mAdapter =
            ArrayAdapter(this, android.R.layout.simple_list_item_1, viewModel.customList)
        etCustom.setAdapter(mAdapter)
        etCustom.setOnItemClickListener { parent, _, position, _ ->
            val itemAtPosition = parent.getItemAtPosition(position) as String
            etCustom.setText(itemAtPosition.split("#")[0])
            tvBom.text = ""
            tvBomDesc.text = ""
            viewModel.getBom(itemAtPosition.split("#")[0])
            bomPop = null
        }
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.flInTime -> {
                dateType = 1
                mDatePicker?.show(System.currentTimeMillis())
            }
            R.id.flCreateTime -> {
                dateType = 2
                mDatePicker?.show(System.currentTimeMillis())
            }
            R.id.flOutTime -> {
//                dateType = 3
//                mDatePicker?.show(System.currentTimeMillis())
            }
            R.id.flUrgent -> {
                if (urgentPop == null) {
                    urgentPop = PopWindowsUtils.getOneChoosePop(
                        this,
                        flUrgent.width,
                        isUrgent,
                        ivUrgent,
                        object :
                            PopWindowsUtils.PopTextAndPositionCallback {
                            override fun onSuccess(codeS: String, potion: Int) {
                                tvUrgent.text = codeS
                            }

                        })

                }

                if (urgentPop!!.isShowing) {
                    urgentPop?.dismiss()
                } else {
                    PopWindowsUtils.setArrowShow(ivUrgent)
                    urgentPop?.showAsDropDown(flUrgent, 0, 0)
                }
            }
            R.id.flFiled -> {
                if (filedPop == null) {
                    filedPop = PopWindowsUtils.getOneChoosePop(
                        this,
                        flFiled.width,
                        viewModel.orderClaesList,
                        ivFiled,
                        object :
                            PopWindowsUtils.PopTextAndPositionCallback {
                            override fun onSuccess(codeS: String, potion: Int) {
                                tvFiled.text = codeS.split("#")[0]

                            }

                        })

                }

                if (filedPop!!.isShowing) {
                    filedPop?.dismiss()
                } else {
                    PopWindowsUtils.setArrowShow(ivFiled)
                    filedPop?.showAsDropDown(flFiled, 0, 0)
                }
            }
            R.id.flCustom -> {
                if (viewModel.customList.isEmpty()){
                    viewModel.getCustom()
                }
                if (customPop == null) {
                    customPop = PopWindowsUtils.getOneChoosePop(
                        this,
                        flCustom.width,
                        viewModel.customList,
                        ivCustom,
                        object :
                            PopWindowsUtils.PopTextAndPositionCallback {
                            override fun onSuccess(codeS: String, potion: Int) {
                                tvCustom.text = codeS.split("#")[0]
                                tvBom.text = ""
                                tvBomDesc.text = ""
                                viewModel.getBom(codeS.split("#")[0])
                                bomPop = null
                            }

                        })

                }

                if (customPop!!.isShowing) {
                    customPop?.dismiss()
                } else {
                    PopWindowsUtils.setArrowShow(ivCustom)
                    customPop?.showAsDropDown(flCustom, 0, 0)
                }
            }
            R.id.flBom -> {
                if (etCustom.text.toString().isEmpty()){
                    viewModel.defUI.toastEvent.value = ToastMessageBean("请先选择客户",false)
                    return
                }

                if (bomPop == null) {
                    bomPop = PopWindowsUtils.getOneChoosePop(
                        this,
                        flBom.width,
                        viewModel.bomList,
                        ivBom,
                        object :
                            PopWindowsUtils.PopTextAndPositionCallback {
                            override fun onSuccess(codeS: String, potion: Int) {
                                tvBom.text = codeS.split("#")[0]
                                tvBomDesc.text = codeS.split("#")[1]
                                viewModel.getProdList(codeS.split("#")[0])
                            }

                        })

                }

                if (bomPop!!.isShowing) {
                    bomPop?.dismiss()
                } else {
                    PopWindowsUtils.setArrowShow(ivBom)
                    bomPop?.showAsDropDown(flBom, 0, 0)
                }
            }
        }
    }
}